<?php
include './header.php';


if (!empty($_GET['info-type'])) {
  $type = $_GET['info-type'];
  $condition = "INNER JOIN nnd_info_type ON nnd_info.info_type = nnd_info_type.info_type_id WHERE info_type = {$type} LIMIT {$offset},{$limit}";
  $info = $db->selectAll('info', '*', $condition);
  $count_cond = "WHERE info_type = {$type}";
  $count = $db->selectOne('info', 'COUNT(*)', $count_cond)[0];
} else {
  $condition = "LIMIT {$offset},{$limit}";
  $info = $db->selectAll('info', '*', $condition);
  $count = $db->selectOne('info', 'COUNT(*)')[0];
}


$page_info = [
    'limit' => $limit,
    'size' => $size,
    'current' => $current,
    'count' => $count,
    'class' => 'pagination'
];
$pageobj = Page::getInstance($page_info);
$page = $pageobj->showpage();


// 删除
if (!empty($_GET['del_info_id'])) {
  $del_id = $_GET['del_info_id'];
  $del_cond = "WHERE info_id = $del_id";
  if ($db->delData('info', $del_cond)) {
    echo "<script>alert('删除成功！');window.location.href='info_list.php'</script>";
  } else {
    echo "<script>alert('删除失败！');window.location.href='info_list.php'</script>";
  }
}


// 批量删除
if ($_POST['del_all']) {
  for ($i = 0; $i < count($_POST['idarr']); $i++) {
    $del_cond = "WHERE info_id = {$_POST['idarr'][$i]}";

    if ($db->delData('info', $del_cond)) {
      echo "<script>alert('删除成功！');window.location.href='info_list.php'</script>";
    } else {
      echo "<script>alert('删除失败！');window.location.href='info_list.php'</script>";
    }
  }
}


?>

<!-- Start: Content -->
<section id="content">
  <div id="topbar" class="affix">
    <ol class="breadcrumb">
      <li><a href="#"><span class="glyphicon glyphicon-home"></span></a></li>
      <li class="active">资讯管理</li>
    </ol>
  </div>
  <div class="container">

    <div class="row">
      <div class="col-md-12">
        <div class="panel">
          <div class="panel-heading">
            <div class="panel-title">资讯列表</div>
            <a href="info_add.php" class="btn btn-info btn-gradient pull-right"><span class="glyphicons glyphicon-plus"></span> 添加文章</a>
          </div>
          <form action="" method="post">
            <div class="panel-body">
              <h2 class="panel-body-title">互联网</h2>
              <table class="table table-striped table-bordered table-hover dataTable">
                <tr class="active">
                  <th class="text-center" width="100"><input type="checkbox" value="" id="checkall" class=""> 全选</th>
                  <th width="150px">标题</th>
                  <th>发布时间</th>
                  <th width="150px">图片</th>
                  <th>作者</th>
                  <th width="200">操作</th>
                </tr>
                <?php foreach ($info as $val) { ?>
                  <tr class="success">
                    <td class="text-center"><input type="checkbox" value="<?php echo $val['info_id']; ?>" name="idarr[]" class="cbox"></td>
                    <td><?php echo $val['info_title']; ?></td>
                    <td><?php echo date('Y-m-d', $val['info_time']); ?></td>
                    <td><img src="<?php echo $val['info_thumb']; ?>" alt=""></td>
                    <td><?php echo $val['info_author']; ?></td>
                    <td>
                      <div class="btn-group">
                        <a href="info_edit.php?info_id=<?php echo $val['info_id']; ?>" class="btn btn-default btn-gradient"><span class="glyphicons glyphicon-pencil"></span></a>
                        <a onclick="return confirm('确定要删除吗？');" href="info_list.php?del_info_id=<?php echo $val['info_id']; ?>" class="btn btn-default btn-gradient dropdown-toggle"><span class="glyphicons glyphicon-trash"></span></a>
                      </div>
                    </td>
                  </tr>
                <?php } ?>
              </table>

              <div class="pull-left">
                <button type="submit" class="btn btn-default btn-gradient pull-right delall" name="del_all" value="del_all"><span class="glyphicons glyphicon-trash"></span></button>
              </div>

              <?php echo $page; ?>

              <!-- <div class="pull-right">
                    <ul class="pagination" id="paginator-example">
                      <li><a href="#">&lt;</a></li>
                      <li><a href="#">&lt;&lt;</a></li>
                      <li><a href="#">1</a></li>
                      <li class="active"><a href="#">2</a></li>
                      <li><a href="#">3</a></li>
                      <li><a href="#">&gt;</a></li>
                      <li><a href="#">&gt;&gt;</a></li>
                    </ul>
                  </div> -->

            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</section>
<!-- End: Content -->
</div>
<!-- End: Main -->
</body>

</html>